package com.hunk.test.problems;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import com.hunk.utils.Print;

/**写一个程序序列化对象，反序列化对象
 * @author HunkDeng
 *
 */
class De_Serializable implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 412705396493311949L;
	String b;
	int d;
	
	@Override
	public String toString() {
		return "De_Serializable [b=" + b + ", d=" + d + "]";
	}

	public static void main(String[] args) {
		De_Serializable writeObject = new De_Serializable();
		writeObject.b = "b";
		writeObject.d = 2;
		//write
		try {
			FileOutputStream fop = new FileOutputStream("De_Serializable.txt");
			DataOutputStream dop = new DataOutputStream(fop);
			ObjectOutputStream oop = new ObjectOutputStream(dop);
			
			oop.writeObject(writeObject);
			oop.flush();
			oop.close();			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		De_Serializable readObject = null;
		//read
		try {
			FileInputStream fip = new FileInputStream("De_Serializable.txt");
			DataInputStream dip = new DataInputStream(fip);
			ObjectInputStream oip = new ObjectInputStream(dip);
			
			readObject = (De_Serializable) oip.readObject();
			oip.close();			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Print.println(readObject);
	}
}
